#ifndef __VIDEO_RECORD_H__
#define __VIDEO_RECORD_H__

#include "common.h"
#include "virtualDevice.h"
#include "cxon2venc.h"
#include "udpCommunication.h"
#include "ppapi.h"

typedef enum {
        IO_METHOD_READ,
        IO_METHOD_MMAP,
        IO_METHOD_USERPTR,
} io_method;

struct buffer {
        void *                  start;
        size_t                  length;
};



#define MAX_VENC_STREAM 2



class VideoRecord
{
public:

    VideoRecord();
    ~VideoRecord();

    int startH264Encode(int argc, char  ** argv);
	void procOneFrame();
	void stopH264Encode();
	void setRc(int stream , int bitrate , int gop);
	void forceIFrame(int stream);

private:
	char *           dev_name;
	io_method        io;
	int              fd;
	struct buffer *         buffers;
	unsigned int     n_buffers;
	int width[MAX_VENC_STREAM] , height[MAX_VENC_STREAM], fps[MAX_VENC_STREAM], bitrate[MAX_VENC_STREAM];
	unsigned image_count;
	unsigned image_encode_count;
	int  nov4l2;


	unsigned smi_fps_div;
	pthread_t threadID;
	pthread_mutex_t smi_mutex;
	pthread_cond_t  smi_cond;
	unsigned int smi_img_addr;
	unsigned main_udp_len;

	unsigned jpg_fps_div;
	pthread_t jpg_threadID;
	pthread_mutex_t jpg_mutex;
	pthread_cond_t  jpg_cond;
	unsigned int jpg_img_addr;
	int jpg_q;
	int jpg_count;

	PPInst pp_inst;
	PPConfig pp_cfg;

	UdpCommunication udp_video_svr[MAX_VENC_STREAM];
	void * venc_handle[MAX_VENC_STREAM];
	struct timeval start_time;
	int enable_test_image;
	FILE * dump_file[MAX_VENC_STREAM];

	void errno_exit  (const char *s);
	int xioctl(int  fd,  int  request, void * arg);
	void process_image(const void * p , unsigned buf_index);
	int read_frame ();
	void run();
	void run_jpg();
	static void * exec(void *arg);
	static void * exec_jpg(void *arg);
	void udp_send(int stream , unsigned char * udp_buf , int udp_len);
	void stopCapturing();
	void startCapturing();
	void uninitDevice();
	void initRead(unsigned int buffer_size);
	void initMmap();
	void initUserp(unsigned int  buffer_size);
	void initDevice();
	void close_device();
	void open_device();
	void setupOn2(int outfmt);
	void encode_small_image(unsigned int img_addr);
	void encode_jpg_image(unsigned int img_addr);
};

#endif
